package com.bkgy.modular.tbcal2130cpcupdatetime.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.modular.tbcal2130cpcupdatetime.entity.TbCal2130CpcUpdateTime;
import com.bkgy.modular.tbcal2130cpcupdatetime.service.TbCal2130CpcUpdateTimeService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/tbCal2130CpcUpdateTime")
public class TbCal2130CpcUpdateTimeController {

    @Resource
    private TbCal2130CpcUpdateTimeService tbCal2130CpcUpdateTimeService;

    @PostMapping("/insert")
    public ResultData insert(@RequestBody String data){
        try {
            TbCal2130CpcUpdateTime tbCal2130CpcUpdateTime = JSONObject.parseObject(data, TbCal2130CpcUpdateTime.class);
            tbCal2130CpcUpdateTimeService.save(tbCal2130CpcUpdateTime);
            return ResultData.ResultDataSuccessSelf("添加成功", null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("添加失败,失败信息为" + e.getMessage(), null);
        }
    }

    @PostMapping("/deleteById")
    public ResultData deleteById(@RequestBody JSONObject json){
        try {
            Integer indocno = json.getInteger("indocno");
            tbCal2130CpcUpdateTimeService.removeById(indocno);
            return ResultData.ResultDataSuccessSelf("删除成功", null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null);
        }
    }

    @PostMapping("/updateById")
    public ResultData updateById(@RequestBody String data){
        try {
            TbCal2130CpcUpdateTime tbCal2130CpcUpdateTime = JSONObject.parseObject(data, TbCal2130CpcUpdateTime.class);
            tbCal2130CpcUpdateTimeService.updateById(tbCal2130CpcUpdateTime);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null);
        }
    }


    @PostMapping("/findByPage")
    public ResultData findByPage(@RequestBody JSONObject json){
        try {
            Integer pageIndex = json.getInteger("pageIndex");
            Integer pageSize = json.getInteger("pageSize");
            Page<TbCal2130CpcUpdateTime> page = new Page<>(pageIndex,pageSize);
            Page<TbCal2130CpcUpdateTime> data = tbCal2130CpcUpdateTimeService.page(page);
            return ResultData.ResultDataSuccess(data.getRecords(), (int) data.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

}
